package com.test.webfluxhtml.common;

import org.apache.commons.lang3.StringUtils;
import com.test.webfluxhtml.config.AvatarImageConfig;
import org.slf4j.LoggerFactory;

import java.util.Arrays;


public class CommonTools {

    /**
     * 用户身份验证
     * @param roleName 角色名
     * @param token 角色证书
     * @return 认证结果及异常信息
     */
    public static CommonResult authUser(String roleName, String token)
    {

        if(StringUtils.isBlank(roleName)||StringUtils.isBlank(token))
        {
            return  new CommonResult(0,"参数错误");
        }
        try
        {
//            MAuthResult result = tokenAuthService.authOurgameUser(roleName,token);
//            if(!result.isAuth())
//            {
//                return new CommonResult(0,"认证失败");
//            }
        }catch (Exception ex)
        {
            LoggerFactory.getLogger(CommonTools.class).error("进行权限验证出现异常",ex);
            return new CommonResult(0,"出现异常，请重试");
        }
        return  new CommonResult(1,"SUCCESS");
    }


    public static String getImageFileExt(String contentType)
    {
        String result ="";
        String[] typeInfo = contentType.split("/");
        if(typeInfo.length>2)
        {
            return typeInfo[1];
        }
        if(typeInfo.length==2)
        {
            switch (typeInfo[1].toLowerCase())
            {
                case "jpeg":
                    return "jpg";
                default:
                    return  typeInfo[1];
            }
        }
        if(typeInfo.length>0)
        {
            return  typeInfo[0];
        }
        return  result;

    }


    /**
     * 获取上传文件支持信息
     * @param fileExt 文件的扩展名称
     * @return 是否支持上传文件的类型
     */
    public static boolean getImageSupport(String fileExt, AvatarImageConfig avatarImageConfig)
    {
        String supportType = avatarImageConfig.getImageTypeSupport();
        String[] supportTypeArray = supportType.split(",");
        if(Arrays.asList(supportTypeArray).contains(fileExt.toLowerCase()))
        {
            return true;
        }
        return false;
    }
}
